package forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class VenueFormInsert extends ActionForm{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * First we provide initial values for all of the input fields that have names matching the property names of the bean, with appropriate getters and setters.
	 */
	
	private String venueName=null;
	private String venueAddress=null;
	private String venueDescription=null;
	private String venueCapacity=null;
	
	public String getVenueName() {
		return venueName;
	}
	public void setVenueName(String name) {
		this.venueName = name;
	}
	public String getVenueCapacity() {
		return venueCapacity;
	}
	public void setVenueCapacity(String capacity) {
		this.venueCapacity = capacity;
	}
	public String getVenueAddress() {
		return venueAddress;
	}
	public void setVenueAddress(String address) {
		this.venueAddress = address;
	}
	public String getVenueDescription() {
		return venueDescription;
	}
	public void setVenueDescription(String description) {
		this.venueDescription = description;
	}
	
	/**
	 * Reset all properties to their default values.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setVenueName(null);
		setVenueCapacity(null);
		setVenueAddress(null);
		setVenueDescription(null);
	}

	/**
	 * Validate the input data. If validation fails, then report the errors in
	 * createevent.jsp, and specifically where <html:errors/> is located.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
	
		ActionErrors errors = new ActionErrors();
		
		String name=request.getParameter("venueName");
		String address=request.getParameter("venueAddress");
		String description=request.getParameter("venueDescription");
		String capacityString=request.getParameter("venueCapacity");
		int capacity = -1; 
		
		if(checkIfStringIsNotValid(name)){
			//errorMessages.put("name", "Please enter a valid venue name");
			errors.add("venueName", new ActionMessage("errors.required", "Venue name"));
		}
		
		if(checkIfStringIsNotValid(address)){
			//errorMessages.put("address", "Please enter a valid address");
			errors.add("venueAddress", new ActionMessage("errors.required", "Venue address"));
		}
		
		if(checkIfStringIsNotValid(description)){
			//errorMessages.put("description", "Please enter a valid description");
			errors.add("venueDescription", new ActionMessage("errors.required", "Venue description"));
		}
		
		if(checkIfStringIsNotValid(capacityString)){
			//errorMessages.put("venuecapacity","Please enter a valid capacity");
			errors.add("venueCapacity", new ActionMessage("errors.required", "Venue capacity"));
		}
		else{
			try{
				capacity=Integer.parseInt(capacityString.trim());
				if(capacity<=0) {
					errors.add("venueCapacity", new ActionMessage("errors.invalid", "Venue capacity"));
				}
			}catch(Exception e){
				errors.add("venueCapacity", new ActionMessage("errors.notanumber", "Venue capacity"));
			}
		}
		
		return errors;
	}


	private boolean checkIfStringIsNotValid(String input){
		if(input==null || input.trim().isEmpty()){
			return true;
		}
		return false;
	}
	
}
